<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport"
		content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
	<title>title</title>
	<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css">
	<link rel="stylesheet" type="text/css" href="../css/api.css" />
	<link rel="stylesheet" type="text/css" href="../css/aui.css" />
	<link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
	<link rel="stylesheet" type="text/css" href="../css/icons.css" />
	<script src="../script/jquery-1.9.1.min.js"></script>
	<script src="../script/bootstrap.min.js"></script>
	<style>
		.table tr:first-child {
			background: url(static/img/tableheader-bg.gif) no-repeat left top;
			background-color: #DCE9F2;
			border-top: 1px solid #CBD6DE;
			border-bottom: 1px solid #ADBECB;
			color: #467aa7;
			font-size: 13px;
			font-weight: bold;
		}

		.table {
			margin-bottom: 15px;
			width: 100%;
			border-collapse: collapse;
			font-size: 13px;
			margin-bottom: 15px;
			width: 100%;
			border-collapse: collapse;
		}

		.table td {
			height: 40px;
			text-align: center;
			border: 1px solid #CBD6DE;
			padding: 0;
			margin: 0;
			vertical-align: middle !important;
		}
	</style>
</head>

<body>
	<header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
		<a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
			<span class="aui-iconfont aui-icon-left"></span>
		</a>
		<div class="aui-title" id="titleX">
			设备点检
		</div>
	</header>
	<div class="aui-content" style="margin-top:50px;">

		<div class="aui-form">

			<div class="aui-content aui-margin-b-15">
				<ul class="aui-list aui-form-list" style="font-size: 14px;">
					<li class="aui-list-item">
						<div class="aui-list-item-inner">
							<div class="aui-list-item-label" style="font-size: 14px;">设备编码</div>
							<input type="text" class="aui-input" placeholder="请 扫描/输入 设备编码"
								style="width: 80%;font-size: 14px;" id="equipCode" disabled="true">
						</div>
					</li>
					<li class="aui-list-item">
						<div class="aui-list-item-inner">
							<div class="aui-list-item-label" style="font-size: 14px;">设备名称</div>
							<input type="text" class="aui-input" placeholder="请 输入 设备名称"
								style="width: 80%;font-size: 14px;" id="equipName" disabled="true">
						</div>
					</li>
					<li class="aui-list-item">
						<div class="aui-list-item-inner">
							<div class="aui-list-item-label" style="font-size: 14px;">点检任务</div>
							<input type="text" class="aui-input" placeholder="选择点检任务"
								style="width: 80%;font-size: 14px;" id="chkTaskCode" value="" disabled="true">
						</div>
					</li>
					<li class="aui-list-item">
						<div class="aui-list-item-inner">
							<div class="aui-list-item-label" style="font-size: 14px;">执行结果</div>
							<div class="aui-list-item-input">
								<select id="zxResult" style="font-size: 14px;">
									<option value="0">未判定</option>
									<option value="1">正常</option>
									<option value="2">异常</option>
								</select>
							</div>
						</div>
					</li>
					<li class="aui-list-item">
						<div class="aui-list-item-inner">
							<div class="aui-list-item-label" style="width:30%;font-size: 14px;">实际开始时间</div>
							<div class="aui-list-item-input" style="float:left;width: 70%;">
								<input type="text" id="actualBeginDate" disabled="true">
							</div>
						</div>
					</li>
				</ul>
			</div>
			<ul id="myTab" class="nav nav-tabs" style="background-color: #F4F4F4;font-size: 13px;">
				<li class="active" style="width: 33.33%;text-align:center;"> <a href="#item" data-toggle="tab">检修项 </a>

				</li>
			</ul>
			<div id="myTabContent" class="tab-content">
				<div class="tab-pane fade in active" id="item">
					<!-- <div style="background-color:white; height: 100%;width: 100%;"> -->
					<div
						style="background-color:white; width: 100%;margin-top:2%;white-space: nowrap;overflow-x: auto;-webkit-overflow-scrolling:touch;">
						<table id="chkItemTable" class="table" style='width: 100%'>

							<tbody id="chkItemTableHead"
								style="display: block; overflow-x: auto; white-space: nowrap;width: 200%">

							</tbody>

						</table>
						<div class="aui-content">


						</div>
					</div>
					<div class="aui-content aui-content-padded" style="text-align:center;margin-bottom: 5%">
						<p>
						</p>
						<div class="aui-btn aui-btn-info" onclick="doSave()" id="submitButton"
							style="width: 100%;height: 100%;">
							<span class="aui-iconfont" style="font-size: 20px;">提交数据</span>

						</div>
						<p></p>
					</div>
				</div>
			</div>
		</div>
	</div>

</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript" src="../script/clickThrottle.js"></script>
<script>

	var taskId = "";
	var taskCode = "";
	var itemIds = "";
	var taskItemIds = ""
	var itemCodes = "";
	var itemNames = "";
	var itemModes = "";
	var remarks = ""
	var itemValues = "";
	var josnList = [];
	var delItemIds = "";
	var str = "";
	var strHC = "";
	var strYQ = "";
	//检测维修耗材参数
	var HCId = "";
	var mrlGid = "";
	var mrlCode = "";
	var mrlName = "";
	var mrlExName = "";
	var qty = "";
	var measureName = "";
	var consumeType = "";
	var remark = "";
	var josnHCList = [];
	var delHCIds = "";

	//检测维修耗材仪器
	var YqId = "";
	var mrlYqGid = "";
	var mrlYqCode = "";
	var mrlYqName = "";
	var remarkYq = "";
	var josnYqList = [];
	var delYqIds = "";

	var YqIds = "";
	var chkModeHave 

	var myDate = new Date;
	var year = myDate.getFullYear();
	var mon = myDate.getMonth() + 1;
	var date = myDate.getDate();
	var hours = myDate.getHours();
	var minutes = myDate.getMinutes();
	var seconds = myDate.getSeconds();
	var now = year + "-" + mon + "-" + date + " " + hours + ":" + minutes + ":" + seconds;

	apiready = function () {


		taskId = api.pageParam.taskId;

		var code = api.pageParam.taskCode;//任务单号
		var equipCode = api.pageParam.equipCode;
		var equipName = api.pageParam.equipName;
		var taskResult = api.pageParam.taskResult;
		var actualBeginDate = api.pageParam.actualBeginDate;
		console.log(code, actualBeginDate)
		$("#equipCode").val(equipCode);
		$("#equipName").val(equipName);
		$("#chkTaskCode").val(code);
		$("#actualBeginDate").val(actualBeginDate);
		setEquipTask(taskId, code, equipCode, taskResult);

	}

	function setEquipTask(taskId, code, equipCode, taskResult) {
		console.log(taskId, code, equipCode, taskResult)
		taskId = taskId;
		$("#zxResult").children().remove();
		if (taskResult == 0) {
			var str = "<option value='0' selected='true'>未判定</option><option value='1' >正常</option><option value='2'>异常</option>";
			$("#zxResult").append(str);
		} else if (taskResult == 1) {
			var str = "<option value='0'>未判定</option><option value='1' selected='true'>正常</option><option value='2'>异常</option>";
			$("#zxResult").append(str);
		} else {
			var str = "<option value='0'>未判定</option><option value='1' >正常</option><option value='2' selected='true'>异常</option>";
			$("#zxResult").append(str);
		}
		$("#chkTaskCode").attr("value", code);
		console.log(taskId)
		if ("" != taskId) {
			//选择设备点检任务后，根据任务id获取点检项
			getEquipTaskItemByTask(taskId);
		}
	}


	function getEquipTaskItemByTask(taskId) {
		console.log("111")
		var serverUrl = localStorage.getItem('serverUrl');
		var url = serverUrl + "/app/getChkItemByTask_ac";
		var un = localStorage.getItem('un');

		var params = new Object();
		params.url = url;
		params.method = 'post';
		params.dataType = 'json';
		params.returnAll = 'false';
		params.timeout = 60;

		var data = {
			values: {
				un: un,
				taskId: taskId,
			}
		};

		api.ajax({
			url: url,
			method: 'post',
			dataType: 'json',
			timeout: 300,
			returnAll: false,
			data: data
		}, function (ret, err) {
			if (ret) {
				var data = JSON.stringify(ret);
				var myobj = eval("(" + data + ")");
				//成功
				if (myobj.code == '0') {
					taskItemIds = "";
					itemIds = "";
					itemCodes = "";
					remarks = ""
					itemNames = "";
					itemModes = "";
					itemValues = "";

					josnList = [];
					var list = myobj.list;
					// console.log( JSON.stringify(list))
					chkModeHave = list.find(item => item.chkMode == '1')
					console.log(chkModeHave)
					var html = "";
					var html1 = "";
					html1 += "<tr id='tr1' color: #467aa7;>";
					html1 += "<td style='width:5%' algin='center'>序号</td>";
					html1 += "<td algin='center' style='display:none;'>编号</td>";
					html1 += "<td style='width:30%' algin='center'>判断标准</td>";
					html1 += "<td style='width:15%' algin='center'>检验标准</td>";
					html1 += "<td style='width:10%' algin='center'>检测值</td>";
					if (chkModeHave != undefined) {
						html1 += "<td style='width:10%' algin='center'>定量检测值</td>";
						html1 += "<td style='width:20%' algin='center'>检查项目</td>";
						html1 += "<td style='width:20%' algin='center'>备注</td>";
						html1 += "</tr>"
					} else {
						html1 += "<td style='width:25%' algin='center'>检查项目</td>";
						html1 += "<td style='width:20%' algin='center'>备注</td>";
						html1 += "</tr>"
					}


					$api.html($api.byId("chkItemTableHead"), html1);
					str = "";
					// console.log(JSON.stringify(list))
					if (list != undefined) {
						//遍历
						for (var i = 0; i < list.length; i++) {
							var taskItemId = list[i].sysid
							var itemId = list[i].itemId;
							var itemCode = list[i].itemCode;
							var itemName = list[i].itemName;
							var chkMode = list[i].chkMode;
							var chkValue = list[i].chkValue;
							var chkResult = list[i].chkResult;
							var remark = list[i].remark;
							var banlance = list[i].ext02;
							if (i == 0) {
								taskItemIds = taskItemId
								itemIds = itemId;
								itemCodes = itemCode;
								itemNames = itemName;
								remarks = remark;

							} else {
								taskItemIds = taskItemIds + ',' + taskItemId
								itemIds = itemIds + "," + itemId;
								itemCodes = itemCodes + "," + itemCode;
								itemNames = itemNames + "," + itemName;
								remarks = remarks + "," + remark
							}


							str += "<tr ><td width='5%' algin='center'>" +
								"" + "<input type='checkbox' onclick='setDelItem(\""
								+ itemId + "\");'></td><td algin='center' style='display:none;'>"
								+ itemCode + "</td><td width='30%' algin='center'>"
								+ itemName + "</td><td width='15%' algin='center'>"
								+ banlance + "</td><td width='10%' algin='center'><select οnchange='handleChange()' id='"

							if (chkResult == "1") {
								str += itemId + "'><option value='0'>未判定</option><option value='1' selected='true'>正常</option><option value='2'>异常</option></select>"
							} else if (chkResult == "2") {
								str += itemId + "'><option value='0'>未判定</option><option value='1'>正常</option><option value='2' selected='true'>异常</option></select></td>"
							} else {
								str += itemId + "'><option value='0' selected='true'>未判定</option><option value='1'>正常</option><option value='2'>异常</option></select></td>"
							}
							if (chkModeHave != undefined) {
								str += "<td width='10%' algin='center'><input id='" + itemId + 'y' + "' type='text' value=''></td>"
								str += "<td width='20%' algin='center'>" + remark + "</td>"
								str += "<td width='20%' algin='center'><input id='" + itemId + 'x' + "' type='text' value=''></td>"
									+ "</tr>";
							} else {
								str += "<td width='25%' algin='center'>" + remark + "</td>"
								str += "<td width='20%' algin='center'><input id='" + itemId + 'x' + "' type='text' value=''></td>"
									+ "</tr>";
							}


						}
					}
					$("#chkItemTableHead").append(str);
					handleChangePut()
					//$api.html($api.byId("chkItemTableBody"), str);
				} else if (myobj.code == '1') {
					api.toast({
						msg: myobj.message,
						duration: 3000
					});
				}
			}
		});
	}

	function setDelItem(id) {
		if (delItemIds == "") {
			delItemIds = id;
		} else {
			if (delItemIds.indexOf(id) > 0) {
				//已存在
				var delArr = delItemIds.split(",");
				delItemIds = "";
				for (i = 0; i < delArr.length; i++) {
					if (delArr[i] != id) {
						if (i == 0) {
							delItemIds = delArr[i];
						} else {
							delItemIds = delItemIds + "," + delArr[i];
						}
					}

				}
			} else {
				//不存在
				delItemIds = delItemIds + "," + id;
			}
		}
	}
	function handleChangePut() {
		var itemIdArr = itemIds.split(",");
		for (i = 0; i < itemIdArr.length; i++) {
			document.getElementById(itemIdArr[i]).onchange = function () {
				handleChange()
			};

		}
	}
	function handleChange() {

		console.log("112")
		var itemIdArr = itemIds.split(",");
		var itemValue = "";
		var resultFlagW = "";
		var resultFlagError = "";
		for (i = 0; i < itemIdArr.length; i++) {

			//获取下拉框选中的值
			itemValue = document.getElementById(itemIdArr[i]).value;//选中的值
			if (itemValue == "2") {
				resultFlagError = 'Y'
			}
			if (itemValue == "0") {
				resultFlagW = 'Y'
			}


		}
		console.log("111", resultFlagError, resultFlagW)
		if (resultFlagError == "Y") {
			document.getElementById("zxResult").value = 2
		} else if (resultFlagError != "Y" && resultFlagW == 'Y') {
			document.getElementById("zxResult").value = 0
		} else {
			document.getElementById("zxResult").value = 1
		}
	}
	function doSave(type) {
		if (itemIds != "") {
			//点检项表格中有数据
			setItemValues();
			//提交数据到服务端
			doSubmit(type);
		} else {
			alert("无点检项信息，无法提交数据");
		}
	}

	function setItemValues() {
		var itemIdArr = itemIds.split(",");
		var itemValue = "";
		var un = localStorage.getItem('un');
		for (i = 0; i < itemIdArr.length; i++) {
			var taskItemIdArr = taskItemIds.split(",");
			var itemCodeArr = itemCodes.split(",");
			var itemNameArr = itemNames.split(",");
			var remarkArr = remarks.split(",");
			var chkValue = "";
			var checkResult = "";
			var taskResult = document.getElementById("zxResult").value;

			var remark = document.getElementById(itemIdArr[i] + "x").value;
			var chkValueRemark = chkModeHave == undefined ? "" : document.getElementById(itemIdArr[i] + "y").value;

			//获取下拉框选中的值
			itemValue = document.getElementById(itemIdArr[i]).value;//选中的值
			checkResult = itemValue;


			if (i == 0) {
				itemValues = itemValue;
			} else {
				itemValues = itemValues + "," + itemValue;
			}
			var iObj = new itemObj(itemIdArr[i], itemCodeArr[i], itemNameArr[i], chkValue, checkResult, un, taskResult, remarkArr[i], remark, chkValueRemark, taskItemIdArr[i]);
			josnList.push(iObj);
		}
	}

	function itemObj(chkItemId, chkItemCode, chkItemName, chkValue, checkResult, chkPerson, taskResult, remark, remarkA, chkValueRemark, taskItemId) {
		this.chkItemGid = chkItemId;
		this.chkItemCode = chkItemCode;
		this.chkItemName = chkItemName;
		this.chkValue = chkValue;
		this.checkResult = checkResult;
		this.taskResult = taskResult;
		this.remark = remark;//检查项目
		this.remarkA = remarkA;//备注
		this.chkValueRemark = chkValueRemark
		this.sysid = taskItemId
	}

	function doSubmit() {
		if (!clickThrottle()) {
			return;
		}

		const submitButton = document.getElementById('submitButton');

		submitButton.style.pointerEvents = "none"
		var type = document.getElementById("zxResult").value;

		if (type == '0') {
			api.toast({
				msg: '执行结果不能为未判定！',
				duration: 3000
			});
			submitButton.style.pointerEvents = "auto"
			return;
		}
		api.showProgress();
		var equipCode = document.getElementById("equipCode").value;
		var serverUrl = localStorage.getItem('serverUrl');
		var url = serverUrl + "/app/saveTaskItem_ac";
		var un = localStorage.getItem('un');

		var params = new Object();
		params.url = url;
		params.method = 'post';
		params.dataType = 'json';
		params.returnAll = 'false';
		params.timeout = 60;

		api.showProgress({
			title: '保存中...',
		});

		var data = {
			values: {
				un: un,
				taskId: taskId,
				rowData: JSON.stringify(josnList),
				type: type,
				equipCode: equipCode,
			}
		};

		api.ajax({
			url: url,
			method: 'post',
			dataType: 'json',
			timeout: 300,
			returnAll: false,
			data: data
		}, function (ret, err) {
			if (ret) {
				var data = JSON.stringify(ret);
				var myobj = eval("(" + data + ")");
				//成功
				if (myobj.code == '0') {

					api.hideProgress();
					submitButton.style.pointerEvents = "auto"
					api.toast({
						msg: "保存成功",
						duration: 3000
					});
					api.closeWin();

					api.execScript({

						name: api.pageParam.name,
						script: 'doSearch()'

					});

				} else if (myobj.code == '1') {
					api.hideProgress();
					submitButton.style.pointerEvents = "auto"
					api.toast({
						msg: myobj.message,
						duration: 3000
					});
				}
			} else {
				api.hideProgress();
				submitButton.style.pointerEvents = "auto"
				alert(JSON.stringify(err));
			}
		});
	}
	//返回按钮
	function closeWin() {
		api.closeWin({});
	}
</script>

</html>